package com.bq.gametrade.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bq.gametrade.HttpLink;
import com.bq.gametrade.R;
import com.bq.gametrade.base.BaseActivity;
import com.bq.gametrade.bean.ResponseBean;
import com.bq.gametrade.bean.UserInfoDetails;
import com.bq.gametrade.http.VRHttp;
import com.bq.gametrade.http.VRHttpListener;
import com.bq.gametrade.utils.MD5Util;
import com.bq.gametrade.utils.NetworkUtils;
import com.bq.gametrade.widget.PayPwdEditText;

import java.util.HashMap;
import java.util.Map;

public class ValidationPayPasswordActivity extends BaseActivity{

    private ImageView back;
    private TextView tvTitle;
    private PayPwdEditText payOriginalPassword;
    private Activity activity;
    private UserInfoDetails userInfo;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_validation_paypassword);
        if(application.isLogin()){
            userInfo = application.getUserInfo();
        }else {
            startActivity(new Intent(this, LoginActivity.class));
            finish();
        }
        activity = this;
        initView();
    }

    private void initView(){
        back = serchViewById(R.id.image_back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        tvTitle = serchViewById(R.id.title_name);
        tvTitle.setText("验证支付密码");
        payOriginalPassword = serchViewById(R.id.ed_payOriginalPassword);
        payOriginalPassword.initStyle(R.drawable.paypassword_edit_num_bg, 6, 1f, R.color.gray, R.color.black, 25);
        payOriginalPassword.setOnTextFinishListener(new PayPwdEditText.OnTextFinishListener() {
            @Override
            public void onFinish(String str) {
                if(NetworkUtils.isNetworkAvailable(activity)){
                    validationPassword(MD5Util.md5(str));
                }else {
                    toastShow("你还没有连接网络哦");
                }

            }
        });

    }

    private void validationPassword(final String password){
        Map<String, String> params = new HashMap<>();
        params.put("token",userInfo.token);
        params.put("pay_pwd",password);
        showSpotsDialog("正在验证···");
        VRHttp.sendRequest(activity, HttpLink.VALIDATION_PAYPASSWORD_URL, params, new VRHttpListener() {
            @Override
            public void onSuccess(Object response, boolean isCache) {
                closeSpotsDialog();
                if(response != null){
                    ResponseBean bean = (ResponseBean)response;
                    if(bean.status.equals("0")){
                        Intent intent = new Intent(ValidationPayPasswordActivity.this,SetPayPasswordActivity.class);
                        intent.putExtra("operaType","1");
                        intent.putExtra("oldPayPassword", password);
                        startActivity(intent);
                        finish();
                    }else {
                        toastShow("支付密码不正确！");
                    }
                }else {
                    toastShow("验证支付密码出出错！");
                }

            }

            @Override
            public void onError(String error) {
                super.onError(error);
                closeSpotsDialog();
                toastShow("支付密码不正确！");
            }
        });
    }


}
